<template>
  <Transition name="back-to-top">
    <button 
      v-if="visible"
      class="back-to-top-btn"
      @click="scrollToTop"
      :title="'回到顶部'"
    >
      <i class="fas fa-arrow-up"></i>
      <span class="progress-ring">
        <svg width="56" height="56" class="progress-svg">
          <circle
            cx="28"
            cy="28"
            r="26"
            class="progress-circle"
            :stroke-dasharray="circumference"
            :stroke-dashoffset="strokeDashoffset"
          />
        </svg>
      </span>
    </button>
  </Transition>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'

const visible = ref(false)
const scrollProgress = ref(0)

const circumference = 2 * Math.PI * 26
const strokeDashoffset = computed(() => {
  return circumference - (scrollProgress.value / 100) * circumference
})

let scrollTimer = null

const updateScrollProgress = () => {
  const scrollTop = window.scrollY || document.documentElement.scrollTop
  const docHeight = document.documentElement.scrollHeight - window.innerHeight
  const progress = docHeight > 0 ? (scrollTop / docHeight) * 100 : 0
  
  scrollProgress.value = Math.min(100, Math.max(0, progress))
  visible.value = scrollTop > 300
}

const handleScroll = () => {
  if (scrollTimer) clearTimeout(scrollTimer)
  scrollTimer = setTimeout(updateScrollProgress, 16)
}

const scrollToTop = () => {
  window.scrollTo({
    top: 0,
    behavior: 'smooth'
  })
}

onMounted(() => {
  updateScrollProgress()
  window.addEventListener('scroll', handleScroll, { passive: true })
})

onUnmounted(() => {
  window.removeEventListener('scroll', handleScroll)
  if (scrollTimer) clearTimeout(scrollTimer)
})
</script>

<style scoped>
.back-to-top-btn {
  position: fixed;
  bottom: var(--space-xl);
  right: var(--space-xl);
  width: 56px;
  height: 56px;
  background: linear-gradient(135deg, var(--color-primary), var(--color-secondary));
  color: var(--text-inverse);
  border: none;
  border-radius: var(--radius-full);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--font-size-lg);
  box-shadow: var(--shadow-lg);
  transition: all var(--transition-normal);
  z-index: var(--z-sticky);
  position: relative;
  overflow: hidden;
}

.back-to-top-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, transparent, rgba(255, 255, 255, 0.2));
  border-radius: var(--radius-full);
  opacity: 0;
  transition: opacity var(--transition-fast);
}

.back-to-top-btn:hover::before {
  opacity: 1;
}

.back-to-top-btn:hover {
  transform: scale(1.1) translateY(-2px);
  box-shadow: var(--shadow-xl);
}

.back-to-top-btn:active {
  transform: scale(0.95);
}

.back-to-top-btn i {
  position: relative;
  z-index: 2;
}

.progress-ring {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.progress-svg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transform: rotate(-90deg);
}

.progress-circle {
  fill: none;
  stroke: rgba(255, 255, 255, 0.8);
  stroke-width: 2;
  stroke-linecap: round;
  transition: stroke-dashoffset var(--transition-fast);
}

/* 动画 */
.back-to-top-enter-active,
.back-to-top-leave-active {
  transition: all var(--transition-normal);
}

.back-to-top-enter-from {
  transform: scale(0) rotate(180deg);
  opacity: 0;
}

.back-to-top-leave-to {
  transform: scale(0) rotate(-180deg);
  opacity: 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .back-to-top-btn {
    bottom: var(--space-lg);
    right: var(--space-lg);
    width: 50px;
    height: 50px;
    font-size: var(--font-size-md);
  }
}

@media (max-width: 480px) {
  .back-to-top-btn {
    bottom: var(--space-md);
    right: var(--space-md);
    width: 45px;
    height: 45px;
    font-size: var(--font-size-sm);
  }
}

/* 高对比度支持 */
@media (prefers-contrast: high) {
  .back-to-top-btn {
    border: 2px solid var(--text-inverse);
  }
}

/* 减少动画支持 */
@media (prefers-reduced-motion: reduce) {
  .back-to-top-btn {
    transition: none;
  }
  
  .back-to-top-btn:hover {
    transform: none;
  }
  
  .progress-circle {
    transition: none;
  }
}
</style> 